<?php

use Phalcon\Mvc\Controller;

class BaseController extends Controller
{
    protected $resp;
    protected $jsdata;
    protected $format = null;
    protected $respbuf = null;
    protected $time = null;
    protected $opuid = null;
    protected $session_timeout = 0;


    public function initialize()
    {
        if (!$this->session->has("user")) {
            $this->session_timeout = 1;
        }

        $this->time = explode(" ", microtime());
        $this->view->disable();

        if (!$this->request->getRawBody()) {
            return;
        }
        $this->jsdata = json_decode($this->request->getRawBody(), true);
        //       $this->dolog(Phalcon\Logger::ERROR);
        if (!$this->jsdata) {
            $this->resp['status'] = -10101;
            $this->resp['message'] = 'invalid arguments';
            $this->respbuf = json_encode($this->resp);
            echo $this->respbuf;
            $this->dolog(Phalcon\Logger::ERROR);
            exit;
        } else {
            $this->resp['status'] = 0;
            $this->resp['message'] = 'success';
        }
        $this->opuid = isset($this->jsdata['opuid']) ? $this->jsdata['opuid'] : 'f4';
        global $_OPUID_;
        $_OPUID_ = $this->opuid;
    }

    public function afterExecuteRoute($dispatcher)
    {
        $this->respbuf = json_encode($this->resp);
        $this->response->setContent($this->respbuf)->send();
        $this->dolog(Phalcon\Logger::INFO);
    }

    function dolog($loglevel)
    {
        $now = explode(" ", microtime());

        $cost = intval(($now[0] + $now[1] - $this->time[1] - $this->time[0]) * 1000);
        $s = "[from:" . $_SERVER['REMOTE_ADDR'] . "] [uri:" . $_SERVER['REQUEST_URI'] .
            "] [method:" . $_SERVER['REQUEST_METHOD'] . "] [body:" . $this->request->getRawBody() . "] [resp:$this->respbuf] [cost:$cost]";

        //$this->logger->log(common::trim($s), $loglevel);
        hilog::info($s);
    }
}
